<?php
require('../database/db.php');
$mysql=new mysql();
// 分页
if($_REQUEST['page']!="" && $_REQUEST['pagesize']!=""){
	$de=$_REQUEST['page']*$_REQUEST['pagesize'];
	$size=$_REQUEST['pagesize'];
}else{
	$de=0;
	$size=10;
}
// 分页 end
// 传入经纬度
$longitude=$_REQUEST[longitude];
$latitude=$_REQUEST[latitude];
// 传入经纬度 end
// 区域id
$cityid=$_REQUEST[cityid];
// 区域id end
//$t = getdate(strtotime("-1 month"));
$t = getdate(time());
$sdate = $t[year].'-'.$t[mon].'-'.'01 00:00:00';
$edate = $t[year].'-'.$t[mon].'-'.'31 23:59:59';

if ($longitude && $latitude && $cityid) {
	$sql = "SELECT *, (SQRT(POWER(MOD(ABS(longitude - {$longitude}),360),2) + POWER(ABS(latitude - {$latitude}),2))*160) AS distance FROM `shop` where city=".$cityid." and `recommend`=1  and `open`=1 and `check`=1  ORDER BY distance limit $de,$size";
	$shop=$mysql->getAll($sql);
	$id=count($shop);
	for($i=0;$i<$id;$i++){
		$shopid=$shop[$i][shopid];
		$sql2= "select count(*) as num from goodsorders where addtime>='$sdate' and addtime<='$edate' and shopid=$shopid and state<>0 and state<>4";
		$result=$mysql->getAll($sql2);
		$num[] = $result[0][num];
		$sql3 = "select count(*) as goods_num, sum(xiaoliang) as goods_xiaoliang from goods where shopid = ".$shopid;
		$result3 =$mysql->getAll($sql3);
		$shop[$i]['goods_num'] = 0;
		$shop[$i]['goods_xiaoliang'] = 0;
		if($result3[0] && $result3[0]['goods_num']){
			$shop[$i]['goods_num'] = $result3[0]['goods_num'];
		}
		if($result3[0] && $result3[0]['goods_xiaoliang']){
			$shop[$i]['goods_xiaoliang'] = $result3[0]['goods_xiaoliang'];
		}else{
			$shop[$i]['goods_xiaoliang'] = rand(10,10000);
		}
		$sql4 = "select goodsid,price,images from goods where shopid = ".$shopid." limit 0,3";
		$result4 =$mysql->getAll($sql4);
		$shop[$i]['good_list'] = $result4;

	}
    //精选商家
    /*if($de==0){
        $sql = "SELECT *, (SQRT(POWER(MOD(ABS(longitude - {$longitude}),360),0) + POWER(ABS(latitude - {$latitude}),0))*160) AS distance  FROM shop WHERE jingxuan=1";
	    $jx_shop=$mysql->getAll($sql);
	    $shop = array_merge($jx_shop, $shop);
    }*/

	$arr = array('code' => 200, 'msg' =>'查询成功' ,'shop' =>$shop,'number'=>$num);
}else{
	$arr = array('code' => -100, 'msg' =>'参数传入有误');
}

echo json_encode($arr);
?>